package test.org.jrfe;

import org.jrfe.JRFE;
import org.jrfe.gfx.BlendFunc;
import org.jrfe.gfx.ColorRGBA;
import org.jrfe.gfx.JRFERenderer;
import org.jrfe.gfx.Vertex;
import org.jrfe.gfx.VertexBatch;
import org.jrfe.gfx.g2d.shapes.Circle;
import org.jrfe.math.Vector2f;
import org.jrfe.res.JRFEResource;
import org.jrfe.sys.JRFEContext;
import org.jrfe.sys.JRFEInput;
import org.jrfe.sys.JRFESettings;
import org.jrfe.sys.JRFESystem;
import org.jrfe.util.FastMath;
import org.lwjgl.input.Keyboard;

public class DrawParabol implements JRFEContext {

	VertexBatch axis = new VertexBatch(4);

	Circle startPoint = new Circle(new Vector2f(), 5, 3);
	Circle endPoint = new Circle(new Vector2f(), 5, 3);
	Vector2f vStartPoint = new Vector2f();
	Vector2f vEndPoint = new Vector2f();

	Rocket r = new Rocket();

	@Override
	public boolean initialize() {

		Vertex vert = new Vertex();

		vert.a = 1f;
		vert.b = 0;

		axis.setPtype(JRFE.PrimitiveTypes.LINES);

		vert.r = 1;
		vert.g = 0;

		vert.x = -800;
		vert.y = 0;
		axis.update(0, vert);

		vert.x = 800;
		vert.y = 0;
		axis.update(1, vert);

		vert.r = 0;
		vert.g = 1;

		vert.x = 0;
		vert.y = -600;
		axis.update(2, vert);

		vert.x = 0;
		vert.y = 600;
		axis.update(3, vert);

		vert.r = 1;
		vert.g = 1;
		vert.b = 1;

		startPoint.setColor(ColorRGBA.MAGENTA);
		endPoint.setColor(ColorRGBA.YELLOW);

		vStartPoint.set(-30, 0);
		vEndPoint.set(30, 0);

		Keyboard.enableRepeatEvents(false);

		JRFERenderer.applyBlending(BlendFunc.SIMPLE);
		return true;
	}

	@Override
	public boolean render() {

		JRFERenderer.beginScene();
		// GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_LINE);

		JRFERenderer.pushMatrix();
		JRFERenderer.translate(400, 300);
		// JRFERenderer.scale(10, 10, 1);
		// v.setTexture(t.getTextureID());
		JRFERenderer.renderVertexBatch(axis);
		JRFERenderer.pushMatrix();
		JRFERenderer.translate(vStartPoint.x, vStartPoint.y);
		JRFERenderer.renderShape(startPoint);
		JRFERenderer.popMatrix();

		JRFERenderer.pushMatrix();
		JRFERenderer.translate(vEndPoint.x, vEndPoint.y);
		JRFERenderer.renderShape(endPoint);
		JRFERenderer.popMatrix();

		r.render();

		JRFERenderer.popMatrix();

		// System.out.println(Mouse.getX() +" " + Mouse.getY());

		JRFERenderer.endScene();

		return true;
	}

	@Override
	public boolean update() {

		if (JRFEInput.isKeyDownHW(JRFE.KeyCodes.KEY_RIGHT)) {
			r.velocity++;
			r.recalculateVelocies();

			System.out.println(r.velocity);
		} else if (JRFEInput.isKeyDownHW(JRFE.KeyCodes.KEY_LEFT)) {
			r.velocity--;
			r.recalculateVelocies();

			System.out.println(r.velocity);
		} else if (JRFEInput.isKeyDownHW(JRFE.KeyCodes.KEY_DOWN)) {
			if (r.angle < 90) {
				r.angle++;
				r.recalculateVelocies();
				System.out.println(r.angle);
			}
		} else if (JRFEInput.isKeyDownHW(JRFE.KeyCodes.KEY_UP)) {
			if (r.angle > 0) {
				r.angle--;
				r.recalculateVelocies();
				System.out.println(r.angle);
			}
		}

		else if (JRFEInput.isKeyDownHW(JRFE.KeyCodes.KEY_A)) {
			r.wind--;
			// r.recalculateVelocies();
			System.out.println(r.wind);

		} else if (JRFEInput.isKeyDownHW(JRFE.KeyCodes.KEY_S)) {
			r.wind++;
			r.recalculateVelocies();
			System.out.println(r.wind);
		}

		float delta = JRFESystem.getDelta();
		r.update(delta);
		// Vertex vert = new Vertex();

		if (JRFEInput.isKeyDownHW(JRFE.KeyCodes.KEY_ESCAPE)) {
			return false;
		}

		return true;
	}

	@Override
	public void cleanup() {
		// TODO Auto-generated method stub
	}

	public static void main(String[] args) throws InterruptedException {

		JRFESettings set = new JRFESettings();
		set.fpslimit = 60;
		// set.bpp = 32;
		JRFESystem.createDisplay(set);
		JRFEResource.initilizeTextureManager();
		JRFESystem.setJRFEContext(new DrawParabol());
		JRFESystem.start();
		JRFEResource.destroyTextureManager();
		JRFESystem.destroyDisplay();

	}
}

class Rocket {

	public float angle = 30;

	public float velocity = 10;

	public float hvelocity = velocity
			* FastMath.sin(angle * FastMath.DEG_TO_RAD);

	public float wind = 0;

	public float vvelocity = velocity
			* FastMath.cos(angle * FastMath.DEG_TO_RAD);

	float maxH = FastMath.pow(velocity, 2) / (2 * FastMath.g);

	//float w = FastMath.pow(velocity, 2) / FastMath.g;

	float d = 0;

	boolean up = true;

	Vector2f loc = new Vector2f();
	Circle circle = new Circle(new Vector2f(), 3, 2);

	public Rocket() {
		circle.setColor(ColorRGBA.PINK);
		loc.set(0, 200);
	}

	public void recalculateVelocies() {
		hvelocity = velocity * FastMath.sin(angle * FastMath.DEG_TO_RAD);
		vvelocity = velocity * FastMath.cos(angle * FastMath.DEG_TO_RAD);
	}

	public void update(float delta) {

		d = d + delta * 10;

		loc.y = vvelocity * d - (FastMath.g * FastMath.pow(d, 2)) / 2;

		//hvelocity
		
		hvelocity = hvelocity + (wind / 1000) * d;
		
		loc.x = hvelocity * d;

		if (loc.y < 0) {
			d = 0;
			recalculateVelocies();
		}
	}

	public void render() {

		JRFERenderer.pushMatrix();
		JRFERenderer.translate(loc.x, loc.y);
		JRFERenderer.renderShape(circle);
		JRFERenderer.popMatrix();

	}

}
